<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
// import { useRouter } from "vue-router";
// let routerr = useRouter();

import router,{ getAuthToken } from "./router/index"
import { generatorRoutes } from "./api/generatorRoutes";
import store from "./store";
export default {
  name: 'App',
  mounted() {
    if (window.performance) {
      const navigationEntries = performance.getEntriesByType('navigation');
      console.log(navigationEntries);
      if (navigationEntries.length > 0) {
        if (navigationEntries[0].type === 'reload') {
          store.commit("addRoutersData", JSON.parse(localStorage.getItem("routesData")));
          generatorRoutes(JSON.parse(localStorage.getItem("routesData")))
          this.$router.push(navigationEntries[0].name.split('#')[1])
          return;
        } else if (navigationEntries[0].type === 'navigate') {
          store.commit("addRoutersData", JSON.parse(localStorage.getItem("routesData")));
          generatorRoutes(JSON.parse(localStorage.getItem("routesData")))
          //  window.location.href = "/";
        }
      }
    }
    // if (localStorage.getItem("routesData")) {
    //   const routes = JSON.parse(localStorage.getItem("routesData"));
    //   store.commit("permission/SET_ROUTES", routes);
    //   router.addRoute(routes); // 或 router.addRoute()
    // }
    const authToken = getAuthToken(); // 读取Token
    if (authToken) {
      // 尝试用Token自动登录
      axios.get("/api/auto-login", {
        headers: { "Authorization": `Bearer ${authToken}` }
      }).then(response => {
        if (response.data.success) {
          // 自动登录成功，跳转到首页
          window.location.href = "/";
        }
      }).catch(error => {
        // Token无效，清除存储
        document.cookie = "authToken=; max-age=0"; // 删除Cookie
        localStorage.removeItem("authToken");
      });
    }

  }
}
</script>

<style>
body,
div {
  margin: 0;
  padding: 0;
}

:root {
  --el-index-normal: none !important;
}

/* #app {
  height: 100vh;
} */
</style>